package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    //    ---------------------------  根据条件分页查询房间列表 ---------------------------
    @Autowired
    RoomInfoMapper roomInfoMapper;
    //    ------------------------------------------------------

    //    ---------------------------   根据id获取房间详细信息 ---------------------------
//    房间对应的公寓信息
    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;
    //   房间图片信息
    @Autowired
    GraphInfoMapper graphInfoMapper;
    //    房间设施信息
    @Autowired
    FacilityInfoMapper facilityInfoMapper;
    //    房间租金信息
    @Autowired
    PaymentTypeMapper paymentTypeMapper;
    //    房间租赁期限信息
    @Autowired
    LeaseTermMapper leaseTermMapper;
    //    房间标签信息
    @Autowired
    LabelInfoMapper labelInfoMapper;
    //    房间属性信息
    @Autowired
    AttrValueMapper attrValueMapper;
    //    ------------------------------------------------------
    //    ---------------------------   保存或更新房间信息 ---------------------------
    @Autowired
    RoomFacilityService roomFacilityService;
    @Autowired
    RoomLabelService roomLabelService;
    @Autowired
    RoomAttrValueService roomAttrValueService;
    @Autowired
    RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    RoomLeaseTermService roomLeaseTermService;
    @Autowired
    GraphInfoService graphInfoService;
    //    ------------------------------------------------------
//    ---------------------------   根据id删除房间信息 ---------------------------
    @Autowired
    LeaseAgreementService leaseAgreementService;

    //    ------------------------------------------------------

    //    根据条件分页查询房间列表
    @Override
    public IPage<RoomItemVo> pageItem(Page<RoomItemVo> roomItemVoPage, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(roomItemVoPage, queryVo);
    }

    //    根据id获取房间详细信息
    @Override
    public RoomDetailVo getDetailById(Long id) {
        //1.查询RoomInfo所有房间信息
        RoomInfo roomInfo = this.getById(id);
        //2.查询RoomInfo对应的ApartmentInfo公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
//        3.查询RoomInfo对应的GraphInfo图片信息
        List<GraphVo> graphVoList = graphInfoMapper.selectGraphByApartId(ItemType.ROOM, id);
//        4.查询RoomInfo对应的FacilityInfo设施信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectFacilityByRoomId(id);
//        5.查询RoomInfo对应的PaymentType租金信息
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectPaymentTypeByRoomId(id);
//        6.查询RoomInfo对应的LeaseTerm租赁期限信息
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectLeaseTermByRoomId(id);
//        7.查询RoomInfo对应的LabelInfo标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.selectLabelByRoomId(id);
//        8.查询RoomInfo对应的AttrValue属性信息
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectAttrValueByRoomId(id);
//        创建RoomDetailVo对象
        RoomDetailVo roomDetailVo = new RoomDetailVo();
//        将RoomInfo的属性值拷贝到RoomDetailVo中
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
//        9.将查询到的租赁期限数据封装到RoomDetailVo中
        roomDetailVo.setLeaseTermList(leaseTermList);
//        10.将查询到的公寓信息数据封装到RoomDetailVo中
        roomDetailVo.setApartmentInfo(apartmentInfo);
//        11.将查询到的租金信息数据封装到RoomDetailVo中
        roomDetailVo.setPaymentTypeList(paymentTypeList);
//          12.将查询到的标签信息数据封装到RoomDetailVo中
        roomDetailVo.setLabelInfoList(labelInfoList);
//        13.将查询到的图片信息数据封装到RoomDetailVo中
        roomDetailVo.setGraphVoList(graphVoList);
//        14.将查询到的属性信息数据封装到RoomDetailVo中
        roomDetailVo.setAttrValueVoList(attrValueVoList);
//        15.将查询到的设施信息数据封装到RoomDetailVo中
        roomDetailVo.setFacilityInfoList(facilityInfoList);
//        16.返回RoomDetailVo对象
        return roomDetailVo;
    }

    //    保存或更新房间信息
    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        //确定是不是修改  房间的id是否为空(不是空的表示->修改)
        boolean isUpdate = roomSubmitVo.getId() != null;
        //修改添加房间信息
        this.saveOrUpdate(roomSubmitVo);
        //房间id
        Long roomId = roomSubmitVo.getId();
        if (isUpdate) {
            //删除房间配套关系表
            roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, roomId));
            //删除标签关系
            roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, roomId));
            //删除房间基本属性关系
            roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, roomId));
            //删除房间支付方式关系
            roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, roomId));
            //删除房间租期关系
            roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId, roomId));
            //删除房间图片关系
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId, roomId).eq(GraphInfo::getItemType, ItemType.ROOM));
        }

//        保存新的facilityInfoList
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
//        判断facilityInfoIds是否为空
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
//            创建一个RoomFacility的集合
            List<RoomFacility> roomFacilities = new ArrayList<>();
//            遍历facilityInfoIds
            for (Long facilityInfoId : facilityInfoIds) {
//                创建一个RoomFacility对象，并设置roomId和facilityId
                RoomFacility roomFacility = RoomFacility.builder().roomId(roomId).facilityId(facilityInfoId).build();
//                添加到roomFacilities集合中
                roomFacilities.add(roomFacility);
            }
//            保存到数据库
            roomFacilityService.saveBatch(roomFacilities);
        }
//        保存新的graphVoList
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
//        判断graphVoList是否为空
        if (!CollectionUtils.isEmpty(graphVoList)) {
//            创建一个GraphInfo的集合
            ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
//            遍历graphVoList
            for (GraphVo graphVo : graphVoList) {
//                创建一个GraphInfo对象，并设置itemType、itemId、name和url
                GraphInfo graphInfo = new GraphInfo();
//               设置类型
                graphInfo.setItemType(ItemType.ROOM);
//                设置id
                graphInfo.setItemId(roomSubmitVo.getId());
//                设置name
                graphInfo.setName(graphVo.getName());
//                设置url
                graphInfo.setUrl(graphVo.getUrl());
//                添加到graphInfoList集合中
                graphInfoList.add(graphInfo);
            }
//            保存到数据库
            graphInfoService.saveBatch(graphInfoList);
        }

//        保存新的attrValueIds
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
//        判断attrValueIds是否为空
        if (!CollectionUtils.isEmpty(attrValueIds)) {
//            创建一个RoomAttrValue的集合
            List<RoomAttrValue> roomAttrValueList = new ArrayList<>();
//            遍历attrValueIds
            for (Long attrValueId : attrValueIds) {
//                创建一个RoomAttrValue对象，并设置roomId和attrValueId
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(attrValueId).build();
//                添加到roomAttrValueList集合中
                roomAttrValueList.add(roomAttrValue);
            }
//            保存到数据库
            roomAttrValueService.saveBatch(roomAttrValueList);
        }

//        保存新的labelInfoIds
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
//        判断labelInfoIds是否为空
        if (!CollectionUtils.isEmpty(labelInfoIds)) {
//            创建一个RoomLabel的集合
            ArrayList<RoomLabel> roomLabelList = new ArrayList<>();
//            遍历labelInfoIds
            for (Long labelInfoId : labelInfoIds) {
//
                RoomLabel roomLabel = RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(labelInfoId).build();
//                添加到roomLabelList集合中
                roomLabelList.add(roomLabel);
            }
//            保存到数据库
            roomLabelService.saveBatch(roomLabelList);
        }

//        保存新的paymentTypeIds
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
//            判断paymentTypeIds是否为空
        if (!CollectionUtils.isEmpty(paymentTypeIds)) {
//            创建一个RoomPaymentType的集合
            ArrayList<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
//            遍历paymentTypeIds
            for (Long paymentTypeId : paymentTypeIds) {
//                创建一个RoomPaymentType对象，并设置roomId和paymentTypeId
                RoomPaymentType roomPaymentType = RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(paymentTypeId).build();
//                添加到roomPaymentTypeList集合中
                roomPaymentTypeList.add(roomPaymentType);
            }
//            保存到数据库
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }

//        保存新的leaseTermIds
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
//            判断leaseTermIds是否为空
        if (!CollectionUtils.isEmpty(leaseTermIds)) {
//            创建一个RoomLeaseTerm的集合
            ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
//            遍历leaseTermIds
            for (Long leaseTermId : leaseTermIds) {
//                创建一个RoomLeaseTerm对象，并设置roomId和leaseTermId
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(leaseTermId).build();
//                添加到roomLeaseTerms集合中
                roomLeaseTerms.add(roomLeaseTerm);
            }
//            保存到数据库
            roomLeaseTermService.saveBatch(roomLeaseTerms);
        }
    }

    //    根据id删除房间信息
    @Override
    public void removeRoomById(Long id) {
        //如果房间是入住状态不能删除 throw new LeaseException(506, "房间已入住不能删除");
        LeaseAgreement one = leaseAgreementService.getOne(new LambdaQueryWrapper<LeaseAgreement>().eq(LeaseAgreement::getRoomId, id).in(LeaseAgreement::getStatus, 2, 5));
        if (one != null) {
            throw new LeaseException(506, "房间已入住不能删除");

        }
        //删除房间基本信息
        this.removeById(id);
        //删除房间配套关系表
        roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, id));
        //删除标签关系
        roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, id));
        //删除房间基本属性关系
        roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, id));
        //删除房间支付方式关系
        roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, id));
        //删除房间租期关系
        roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId, id));
        //删除房间图片关系
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId, id).eq(GraphInfo::getItemType, ItemType.ROOM));
    }
}




